package almacenes.controlador.gestionar;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.event.DateSelectEvent;

import almacenes.modelo.excepcion.ExcepcionGeneral;
import almacenes.modelo.service.InspeccionService;
import almacenes.modelo.service.ProveedorService;
import almacenes.modelo.service.impl.InspeccionServiceImpl;
import almacenes.modelo.service.impl.ProveedorServiceImpl;

@ManagedBean(name="generalMB")
@SessionScoped
public class GeneralMB {
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	protected ProveedorService proveedorService;
	protected InspeccionService inspeccionService;
	protected SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
	protected GeneralMB(){
	}

   protected Object bean ;

	public Object getBean() {
	return bean;
}

   public void setBean(Object bean) {
	this.bean = bean;
   }

	protected Object getBeanInstance() throws ExcepcionGeneral {
		Class clazz;
		Object tmpObject;
		clazz = getBean().getClass();
		try {
			tmpObject = Class.forName(clazz.getName()).newInstance();
		} catch (InstantiationException e) {
			throw new ExcepcionGeneral(e);
		} catch (IllegalAccessException e) {
			throw new ExcepcionGeneral(e);
		} catch (ClassNotFoundException e) {
			throw new ExcepcionGeneral(e);
		}
		return tmpObject;
	}
	
	protected void validarFechaMenor(DateSelectEvent event, Date dtFecRegFin) {
		logger.debug("validarFechaMenor **** " +event.getDate().getTime());
		  FacesContext facesContext = FacesContext.getCurrentInstance();
		  SimpleDateFormat sdfFormatoFecha = new SimpleDateFormat("dd/MM/yyyy");
		  if (dtFecRegFin != null) {
		   if (event.getDate().getTime() > dtFecRegFin.getTime()) {
		    facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
		    		"Fecha de inicio: Fecha debe ser mayor a la fecha final",sdfFormatoFecha.format(event.getDate())));
		    }
		  }
		 }
	
	protected void validarFechaMayor(DateSelectEvent event, Date sFechaInicio) {
		logger.debug("validarFechaMayor **** " +event.getDate().getTime());
		  FacesContext facesContext = FacesContext.getCurrentInstance();
		  SimpleDateFormat sdfFormatoFecha = new SimpleDateFormat("dd/MM/yyyy");
		  if (sFechaInicio != null) {
		   if (event.getDate().getTime() < sFechaInicio.getTime()) {
		    facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
		          "Fecha de fin: Fecha debe ser menor a la fecha inicial",
		          sdfFormatoFecha.format(event.getDate())));
		   }
		  }
	}
	public String capturarParametro(String codigo){
	Map<String, String> paramMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    String strCodigoParametro = paramMap.get(codigo);
    logger.debug("capturarParametro -- Codigo :" +strCodigoParametro);
    return strCodigoParametro;
	}
    public ProveedorService getProveedorService() {
		proveedorService =  new ProveedorServiceImpl();
		return   proveedorService;
	}

	public void setProveedorService(ProveedorService proveedorService) {
		this.proveedorService = proveedorService;
	}

	public InspeccionService getInspeccionService() {
		inspeccionService = new InspeccionServiceImpl();
		return inspeccionService;
	}

	public void setInspeccionService(InspeccionService inspeccionService) {
		this.inspeccionService = inspeccionService;
	}

	
	
	
	
}
